package com.nwpu.bigdata.service.impl;

import java.util.List;

import javax.cache.annotation.CacheResult;

import com.nwpu.bigdata.dao.mapper.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.nwpu.bigdata.model.Rule;
import com.nwpu.bigdata.service.RuleService;

/**
 *@version:2018年7月19日下午10:51:23
*/
/**
 * @author Administrator
 *
 */
@Service
public class RuleServiceImpl implements RuleService{
	private static Logger logger = LoggerFactory.getLogger(RuleServiceImpl.class);
	@Autowired
	RuleMapper ruleMapper;
	@Autowired
	HostMapper hostMapper;
	@Autowired
	MetricMapper metricMapper;
	@Autowired
    CheckFuncMapper functionMapper;
	@Autowired
	UserMapper userMapper;
	
	@Autowired
	MediaMapper mediaMapper;
	@Override
	//@Cacheable修饰的方法是可以缓存的，在第一次执行这个方法时，他的结果过会被缓存下来，在缓存的有效时间内，以后访问这个方法直接从缓存中获取结果，不在执行以下方法。
	//value为缓存位置的名称，key表示使用方法的参数类型及参数值作为key，有两种方法指定key一种是自定义，一种是不指定是默认指定的
	
	//@CacheResult(cacheName = "ruleCache")
	public List<Rule> findRuleDetail() throws Exception {
		
			List<Rule> ruleList = ruleMapper.findRuleDetailResultMap();
		
		    return ruleList;
		
	}
	/* (non-Javadoc)
	 * @see com.nwpu.bigdata.service.RuleService#insert(com.nwpu.bigdata.model.Rule)
	 */
	@Override
	public int insertRule(Rule record) {
		
		//向mysql中插入一条规则
	
		int id =ruleMapper.ruleInsert(record);
		
		return id;
	}
	/* (non-Javadoc)
	 * @see com.nwpu.bigdata.service.RuleService#selectHostId(java.lang.String)
	 */
	@Override
	public Long selectHostId(String hostName) {
		long id = hostMapper.selectHostIdByHostName(hostName);
		
		return id;
	}
	/* (non-Javadoc)
	 * @see com.nwpu.bigdata.service.RuleService#selectMetricId(java.lang.String)
	 */
	@Override
	public Long selectMetricId(String metricName) {
		Long id = metricMapper.selectMetricIdByMetricName(metricName);
		return id;
	}
	
	@Override
	public Long selectTriggerFuncId(String triggerFuncName) {
		Long id = functionMapper.selectFunctionIdByFunctionName(triggerFuncName);
		return id;
	}
    @Override
    public Long selectUserId(String userName){
    	long id = userMapper.selectUserIdByUserName(userName);
    	return id;
    }
    
    @Override
    public byte selectMediaName(String mediaName){
    	byte id = mediaMapper.selectMediaTypeIdByMediaTypeName(mediaName);
    	return id;
    }
	public RuleMapper getRuleMapper() {
		return ruleMapper;
	}
	/* (non-Javadoc)
	 * @see com.nwpu.bigdata.service.RuleService#deleteRule(java.lang.Long)
	 */
	@Override
	public void deleteRule(Long id) {
		ruleMapper.deleteByPrimaryKey(id);
		
	}
}
